package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.VO.AttributeListVO;
import cn.tedu.csmall.product.pojo.VO.AttributeStandardVO;
import cn.tedu.csmall.product.pojo.VO.AttributeTemplateListVO;
import cn.tedu.csmall.product.pojo.VO.AttributeTemplateStandardVO;
import cn.tedu.csmall.product.pojo.dto.AttributeAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateUpdateDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeUpdateDTO;
import cn.tedu.csmall.product.service.IAttributeService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/attributes")
@Api(tags = "属性管理模块")
@Validated
public class AttributeController {

    @Autowired
    IAttributeService attributeService;

    @PostMapping("/add-new")
    @ApiOperation("添加属性")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid AttributeAddNewDTO attributeAddNewDTO){
        attributeService.addNew(attributeAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除属性")
    @ApiOperationSupport(order = 200)
    public JsonResult<Void> delete(@Range(min = 1,max = 10000,message = "删除属性失败，非法ID！") @PathVariable Long id){
        attributeService.deleteById(id);
        return JsonResult.ok();
    }
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改属性")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateInfoById(@PathVariable Long id, AttributeUpdateDTO attributeUpdateDTO){
        attributeService.updateInfoById(id, attributeUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}/select")
    @ApiOperation("查询属性详情")
    @ApiOperationSupport(order = 400)
    public JsonResult<AttributeStandardVO> getStandard(@PathVariable Long id){
        AttributeStandardVO standardById = attributeService.getStandardById(id);
        return JsonResult.ok(standardById);
    }

    @GetMapping("/list")
    @ApiOperation("查询属性列表")
    @ApiOperationSupport(order = 410)
    public JsonResult<List<AttributeListVO>> list(){
        List<AttributeListVO> list = attributeService.list();
        return JsonResult.ok(list);
    }

}
